﻿using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace WenSkin.Controls
{
    public class WenTreeDataGridViewTextBoxCell : DataGridViewTextBoxCell
    {
        private TreeDataGridViewCellValues Values { get; set; } 
        protected override void Paint(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(g, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            if (value is TreeDataGridViewCellValues treeData)
            {
                int indent = treeData.Indent * treeData.Level + 3;

                RectangleF rec = new RectangleF(cellBounds.X, cellBounds.Y + 1, cellBounds.Width - 1, cellBounds.Height - 2);


                ControlHelper.SetGDIHigh(g);

                RectangleF recStr = new RectangleF(rec.X + 16 + indent, rec.Y, rec.Width, rec.Height);

                if ((cellState | DataGridViewElementStates.Selected) == cellState)
                {
                    g.FillRectangle(new SolidBrush(cellStyle.SelectionBackColor), rec);
                    g.DrawString(treeData.Text, cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), recStr, ControlHelper.StringConters);
                }
                else
                {
                    g.FillRectangle(new SolidBrush(cellStyle.BackColor), rec);
                    g.DrawString(treeData.Text, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), recStr, ControlHelper.StringConters);
                }

                #region 其他方法


                //按钮区域
                //Rectangle recButton = new Rectangle((int)rec.X + indent, (int)rec.Y + (int)(rec.Height - 12) / 2, 12, 12);

                //用gdi绘制

                //g.DrawRectangle(Pens.Gray, recButton);

                ////绘制横线一条  -

                //g.DrawLine(Pens.Black, recButton.X+1, recButton.Y + 6, recButton.X + 10, recButton.Y + 6);

                //if (treeData.CellState)
                //    g.DrawLine(Pens.Black, recButton.X + 6, recButton.Y+1, recButton.X + 6, recButton.Y + 10);

                ////画虚线
                //using (Pen p=new Pen(Color.Gray) {DashStyle=System.Drawing.Drawing2D.DashStyle.Dot })
                //{
                //    Rectangle xian=DataGridView.Rows[rowIndex].Cells[]
                //    g.DrawLine(p,)
                //}

                #endregion

                // 采用图片绘制
                Image image = WenSkin.Properties.Resources.sanjiaoxing;
                image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                if (treeData.CellState)
                    image.RotateFlip(RotateFlipType.Rotate270FlipNone);


                g.DrawImage(image, rec.X + indent, rec.Y + (rec.Height - 16) / 2, 16, 16);
            }
        }

        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            base.OnMouseClick(e);

            Rectangle cellRec = this.ContentBounds;

            if (this.Value is TreeDataGridViewCellValues treeData)
            {
                Rectangle rec = new Rectangle(treeData.Level * treeData.Indent
                    , (cellRec.Height - 16) / 2, 16, 16);
                if (rec.Contains(e.Location))
                {
                    if (!treeData.CellState)
                    {
                        CloseSubNode(treeData.Nodes, e.ColumnIndex);
                        treeData.CellState = true;
                    }
                    else
                    {
                        foreach (TreeDataGridViewRowNode item in treeData.Nodes)
                        {
                            item.DataGridViewRow.Visible = true;
                        }
                        treeData.CellState = false;
                    }
                }
            }
        }
        private void CloseSubNode(TreeDataGridViewRowNodeCollection nodes, int columnIndex)
        {
            foreach (TreeDataGridViewRowNode item in nodes)
            {
                item.DataGridViewRow.Visible = false;
                if (item.DataGridViewRow.Cells[columnIndex].Value is TreeDataGridViewCellValues values)
                {
                    values.CellState = true;
                }
                if (item.Nodes.Count > 0)
                    CloseSubNode(item.Nodes, columnIndex);
            }
        }

        //值变化
        protected override bool SetValue(int rowIndex, object value)
        {
            if (value is TreeDataGridViewCellValues treeData)
            {
                Values = treeData;
                return base.SetValue(rowIndex, value);
            }
            else if (Values is TreeDataGridViewCellValues v)
            {
                v.Text = value?.ToString();
                return base.SetValue(rowIndex, this.Values);
            }
            return base.SetValue(rowIndex, value);
        }
    }
}
